"""
Case Type   : bugfix
Case Name   : 创建用户查看当前schema以及schema所属owner
Create At   : 2025/6/25
Owner       : liu-tong-8848
Description :
    1、创建用户
    2、查看当前schema
    3、清理环境
Expect      :
    1、成功
    2、成功，创建用户会同时创建同名schema，且此schema的属主为此用户
    3、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:创建用户；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop user if exists test_usr_061;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, sql_res, '执行失败' + text)

        sql_cmd = f"create user test_usr_061 password \'Huawei@123\' login;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, sql_res, '执行失败' + text)

        text = '----step2:查看当前schema；expect:成功，创建用户会同时创建同名schema，且此schema的属主为此用户----'
        self.log.info(text)
        sql_cmd = f'\\dn'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertNotIn('ERROR', sql_res, '执行失败' + text)
        self.assertIn('test_usr_061    | test_usr_061', sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step3:清理环境；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop user if exists test_usr_061;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
